<?php

namespace App\Http\Admin\Controllers\User;
use App\Services\user\UserAgentServices;
use App\Http\Controllers\Controller;
use Hlcreation\Basic\Helper;
use App\Requests\UserRequest;

/**
 *   代理商管理信息
 * */

class UserAgentController  extends Controller
{

    /**
     * @param UserAgentServices $services
     */
    public function __construct(UserAgentServices $services)
    {

        $this->services = $services;
    }



    /**
     * 代理商列表
     */
    public function  index(){

        $post['limit']=  Helper::getPost('limit',20);
        $name=  Helper::getPost('name','');
        $post['page']=Helper::getPost('page', 1);
        $create_time=Helper::getPost('create_time','');

        $where = [];
        if ($name){
            $where[] = ['account', '=',$name];
        }
        if ($create_time){

            $start_time = $create_time.' 00:00:00';
            $end_time = $create_time.' 23:59:59';


//            $where[] = ['created_at','between',[$start_time,$end_time]];
            $where[] = ['created_at', '>=',$start_time];
            $where[] = ['created_at', '<',$end_time];
            //dd($map);
        }

        $list = $this->services->AgentList($where,$post);

        return Helper::toJson("代理商列表", 200, $list);
    }


    /**
     * 代理商创建
     */
    public  function  save(UserRequest  $request){
        $userInfo = Helper::getPost('userinfo');
        $all = $request->all();
        $all['pwd'] = md5($all['pwd']);
        $all['creator_uid']=$userInfo['user_info']['id'];

        $data = $this->services->AgentSave($all);

        if($data){
            return Helper::toJson("创建成功", 200);
        }else{
            return Helper::toJson("创建失败", 400);
        }
    }


    /**
     * 修改代理商
     */
    public  function  edit(UserRequest  $request){
        $userInfo = Helper::getPost('userinfo');
        $agent_id=Helper::getPost('agent_id');
        $all = $request->all();
        $all['expiration_time'] = strtotime($all['expiration_time']);
        $all['creator_uid']=$userInfo['user_info']['id'];
        $data = $this->services->AgentUpdate($agent_id,$all);

        if($data){
            return Helper::toJson("修改成功", 200);
        }else{
            return Helper::toJson("修改失败", 400);
        }
    }


    /**
     * 获取代理商信息
     */
    public  function  getAgent(){
        $id=Helper::getPost('agent_id');
        $data = $this->services->getAgent($id);
        return Helper::toJson("数据获取成功", 200,$data);
    }


    /**
     * 删除代理商
     */
    public  function   agentDel(){
        $agnet_id=Helper::getPost('agent_id');
        //$user_id=Helper::getPost('user_id');
        $admin_id=Helper::getPost('userinfo')['user_info']['id'];
        $data = $this->services->agentDelte($admin_id,$agnet_id);
        if($data) return Helper::toJson("删除成功", 200);
        else Helper::toJson("删除失败", 400);
    }


    /**
     * 代理商禁用
     */
    public  function  editDisabled(){
        $admin_id=Helper::getPost('userinfo')['user_info']['id'];
        $user_id=Helper::getPost('user_id');
        $status= Helper::getPost('status');
        if($status){
            $this->services->EditDisabled($admin_id,$user_id,1);
            return Helper::toJson("启用成功", 200);
        }else{
            $this->services->EditDisabled($admin_id,$user_id,0);
            return Helper::toJson("禁用成功", 200);
        }

    }

    /**
     * 代理商配置
     */
    public  function   agentConfig(){
        $admin_id=Helper::getPost('userinfo')['user_info']['id'];
        $id=Helper::getPost('user_id');
        $type=Helper::getPost('type',1);
        $status=Helper::getPost('status',0);

        $this->services->agentConfig($admin_id,$type,$id,$status);
        return Helper::toJson("操作成功", 200);
    }



    /**
     * 修改平台数量  店铺数量  短信数量
     */
    public  function   setAgentSmsContract(){
        $admin_id= Helper::getPost('userinfo')['user_info']['id'];
        $agent_id=Helper::getPost('user_id');

        //平台数量  店铺数量  短信数量
        $arr['sms_num']=Helper::getPost('sms_num');
        $arr['shop_num']=Helper::getPost('shop_num');
        $arr['store_num']=Helper::getPost('store_num');
        $data=$this->services->setAgentSmsContract($agent_id,$admin_id,$arr);
        if($data)  return Helper::toJson("设置成功", 200);
    }


}
